package offer.linkedList;

/**
 * @author DengYuan2
 * @create 2021-02-23 19:01
 */
public class n_24 {
    public static void main(String[] args) {
        n_24 n = new n_24();
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        ListNode n6 = new ListNode(6);
        ListNode n7 = new ListNode(7);
        ListNode n8 = new ListNode(8);
        ListNode n9 = new ListNode(9);
        ListNode n10 = new ListNode(10);
        ListNode n11 = new ListNode(11);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        n5.next = n6;
        n6.next = n7;
        n7.next = n8;
        n8.next = n9;
        n9.next = n10;
        n10.next = n11;
        ListNode res = n.ReverseList(n1);
        System.out.println(res);
    }

    /**
     * 我的写法-头插法
     * @param head
     * @return
     */
    public ListNode ReverseList(ListNode head) {
        if (head==null){
            return null;
        }
        ListNode dumb = new ListNode(-1);
        ListNode cur = head;
        ListNode tmp =null;
        while (cur!=null){
            tmp=cur.next;
            cur.next=dumb.next;
            dumb.next=cur;
            cur=tmp;
        }
        return dumb.next;
    }

    /**
     * 大神-递归法
     * @param head
     * @return
     */
    public ListNode ReverseList2(ListNode head){
        if (head==null || head.next==null){
            return head;
        }
        ListNode next = head.next;
        head.next=null;
        ListNode node = ReverseList2(next);
        next.next=head;
        return node;

    }
}
